﻿namespace Sai.Test.Sandcastle
{
	using System.IO;
	using Sai.Core.Sandcastle;
	using Sai.Test.TestDataBuilders;
	using Xunit;
	using System.Collections.Generic;
    using Sai.Test.XUnit;
	
	public class ProjectFacts
	{
		public class TheContentLayoutNameProperty
		{
			
			[Fact, InMemoryTest]
			public void Finds_Name_Of_Layout_From_File()
			{
				var project = SandcastleProjectBuilder.SandcastleProject()
					.WithFile("Help\\First.aml")
					.WithFile("Help\\Second.aml")
					.WithFile("Help\\Third.aml")
					.WithFile("Help\\APIChanges.aml")
					.WithFolder("Help")
					.WithLayout("Project.Content")
					.Build();

				using (var reader = new StringReader(project))
				{
					Project p = Project.LoadFrom(reader);

					Assert.Equal("Project.Content", p.ContentLayoutName);
				}
			}

			
			[Fact, InMemoryTest]
			public void Finds_Fully_Qualified_Name_Of_Layout_From_Folder_And_File()
			{
				var project = SandcastleProjectBuilder.SandcastleProject()
					.WithFile("Help\\First.aml")
					.WithFile("Help\\Second.aml")
					.WithFile("Help\\Third.aml")
					.WithFile("Help\\APIChanges.aml")
					.WithFolder("Help")
					.WithLayout("Project.Content")
					.Build();

				using (var reader = new StringReader(project))
				{
					Project p = Project.LoadFrom(reader);
					p.Folder = "C:\\Help";

					Assert.Equal("C:\\Help\\Project.Content", p.ContentLayoutName);
				}
			}
		}

		public class TheContentProperty
		{
			
			[Fact, InMemoryTest]
			public void Finds_Name_Of_All_Content_From_File()
			{
				var project = SandcastleProjectBuilder.SandcastleProject()
					.WithFile("Help\\First.aml")
					.WithFile("Help\\Second.aml")
					.WithFile("Help\\Third.aml")
					.WithFile("Help\\APIChanges.aml")
					.WithFolder("Help")
					.WithLayout("Project.Content")
					.Build();

				using (var reader = new StringReader(project))
				{
					Project p = Project.LoadFrom(reader);

					var list = p.Content;

					Assert.Equal(4, list.Count);
					Assert.Contains("Help\\First.aml", list);
					Assert.Contains("Help\\Second.aml", list);
					Assert.Contains("Help\\Third.aml", list);
					Assert.Contains("Help\\APIChanges.aml", list);
				}
			}

			
			[Fact, InMemoryTest]
			public void Uses_Absolute_Paths_If_Folder_Known()
			{
				var project = SandcastleProjectBuilder.SandcastleProject()
					.WithFile("Help\\First.aml")
					.WithFile("Help\\Second.aml")
					.WithFile("Help\\Third.aml")
					.WithFile("Help\\APIChanges.aml")
					.WithFolder("Help")
					.WithLayout("Project.Content")
					.Build();

				using (var reader = new StringReader(project))
				{
					Project p = Project.LoadFrom(reader);
					p.Folder = "C:\\Project";

					var list = p.Content;

					Assert.Equal(4, list.Count);
					Assert.Contains("C:\\Project\\Help\\First.aml", list);
					Assert.Contains("C:\\Project\\Help\\Second.aml", list);
					Assert.Contains("C:\\Project\\Help\\Third.aml", list);
					Assert.Contains("C:\\Project\\Help\\APIChanges.aml", list);
				}
			}
		}

		public class TheRemoveContentMethod
		{
			
			[Fact, InMemoryTest]
			public void With_Folder_Specified_Removes_Fully_Qualified_Files()
			{
				var project = SandcastleProjectBuilder.SandcastleProject()
					.WithFile("Help\\First.aml")
					.WithFile("Help\\Second.aml")
					.WithFile("Help\\Third.aml")
					.WithFile("Help\\APIChanges.aml")
					.WithFolder("Help")
					.WithLayout("Project.Content")
					.Build();

				using (var reader = new StringReader(project))
				{
					Project p = Project.LoadFrom(reader);
					p.Folder = "C:\\Project";

					List<string> removeFiles = new List<string>();
					removeFiles.Add("C:\\Project\\Help\\First.aml");
					removeFiles.Add("C:\\Project\\Help\\Second.aml");

					p.RemoveContent(removeFiles);

					var list = p.Content;

					Assert.Equal(2, list.Count);
					Assert.Contains("C:\\Project\\Help\\Third.aml", list);
					Assert.Contains("C:\\Project\\Help\\APIChanges.aml", list);
				}
			}

			
			[Fact, InMemoryTest]
			public void With_Folder_Specified_Adds_Fully_Qualified_Files()
			{
				var project = SandcastleProjectBuilder.SandcastleProject()
					.WithFile("Help\\First.aml")
					.WithFile("Help\\Second.aml")
					.WithFile("Help\\Third.aml")
					.WithFile("Help\\APIChanges.aml")
					.WithFolder("Help")
					.WithLayout("Project.Content")
					.Build();

				using (var reader = new StringReader(project))
				{
					Project p = Project.LoadFrom(reader);
					p.Folder = "C:\\Project";

					List<string> addFiles = new List<string>();
					addFiles.Add("C:\\Project\\Help\\Fourth.aml");
					addFiles.Add("C:\\Project\\Help\\Fifth.aml");
					addFiles.Add("C:\\Project\\Help\\Sixth.aml");
					addFiles.Add("C:\\Project\\Help\\Seventh.aml");

					p.AddContent(addFiles);

					var list = p.Content;

					Assert.Equal(8, list.Count);
					Assert.Contains("C:\\Project\\Help\\First.aml", list);
					Assert.Contains("C:\\Project\\Help\\Second.aml", list);
					Assert.Contains("C:\\Project\\Help\\Third.aml", list);
					Assert.Contains("C:\\Project\\Help\\APIChanges.aml", list);
					Assert.Contains("C:\\Project\\Help\\Fourth.aml", list);
					Assert.Contains("C:\\Project\\Help\\Fifth.aml", list);
					Assert.Contains("C:\\Project\\Help\\Sixth.aml", list);
					Assert.Contains("C:\\Project\\Help\\Seventh.aml", list);

					using (var writer = new StringWriter())
					{
						p.SaveTo(writer);

						string output = writer.ToString();

						Assert.Contains("<Content Include=\"Help\\Fourth.aml", output);
						Assert.Contains("<Content Include=\"Help\\Fifth.aml", output);
						Assert.Contains("<Content Include=\"Help\\Sixth.aml",  output);
						Assert.Contains("<Content Include=\"Help\\Seventh.aml", output);
					}
				}
			}
		}
	}
}
